/**
 * 栈Stack
 * LIFO后进先出，添加或删除的原素都保存在栈的末尾（即栈顶），另一端则为栈尾
 * 模拟栈：
 */
function Stack() {
  var items = []

  /**
   * 添加新元素到栈顶
   */
  this.push = function(element) {
    items.push(element)
  }

  /**
   * 移除栈顶元素,并返回
   */
  this.pop = function(element) {
    return items.pop()
  }

  this.size = function() {
    return items.length
  }

  this.isEmpty = function() {
    return items.length == 0
  }

  this.peek = function() {
    return [items.length - 1]
  }

  this.clear = function() {
    items = []
  }

  this.print = function() {
    console.log(items.toString())
  }
}

function devideBy2(number, base) {
  // typeof base == 'number'
  let stack = new Stack()
  let str = ''
  let baselist = '0123456789ABCDEF'
  while(number > 0) {
    // stack.push(number % 2)
    stack.push(baselist[number % base])
    number = Math.floor(number / base)
    // number = Math.floor(number / 2)
  }
  while(!stack.isEmpty()) {
    str += stack.pop().toString()
  }
  return str
}

console.log(devideBy2(10438, 16))